﻿using System.Windows.Controls;
using System.Windows;
using XAMLStylePad.ViewModels;
using System;
using FireFly;

namespace XAMLStylePad.Views
{
    public class StylePadTree : TreeView
    {
        public StylePadTree() : base()
        {
            this.Resources.MergedDictionaries.Add(new ResourceDictionary()
            {
                Source = new Uri(@"Themes/AppResources.xaml", UriKind.Relative)
            });
            ItemTemplateSelector = new StylePadTreeTemplateSelector(this);
        }

        protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
        {
            base.OnSelectedItemChanged(e);
            var vm = e.NewValue as IViewModel;


            if(vm != null && vm.SelectCommand!=null)
            {
                vm.SelectCommand.Execute(vm);
            }
            
        }

        //protected override bool IsItemItsOwnContainerOverride(object item)
        //{
        //    return item is ProjectViewModel;
        //}
        
        //#region Mode
        //public StylePadTreeMode Mode
        //{
        //    get
        //    {
        //        return (StylePadTreeMode)GetValue(_Mode);
        //    }
        //    set
        //    {
        //        SetValue(_Mode, value);
        //    }
        //}
        //public static readonly DependencyProperty _Mode = DependencyProperty.Register("Mode", typeof(StylePadTreeMode), typeof(StylePadTree)); 
        //#endregion
    }

    public class StylePadTreeTemplateSelector: DataTemplateSelector
    {
        StylePadTree _tree;
        public StylePadTreeTemplateSelector(StylePadTree tree)
        {
            _tree = tree;
        }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if(item is IViewModel)
            {
                var res = (HierarchicalDataTemplate)_tree.Resources["ViewModelItemTemplate"];
                return res;
            }
            return null;
        }
    }

    /// <summary>
    /// Supported Items by StylePropertyeditableTree
    /// </summary>
    public enum StylePadTreeMode
    {
        TopLevels = 1,
        LowLevels = 2,
        All = 3
    }
}
